package com.fjw.controller;

import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.boot.system.ApplicationHome;
import org.springframework.util.ResourceUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;

import java.io.*;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;

import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.text.PDFTextStripper;

@RestController
@RequestMapping("/picture")
public class UploadController {

    @PostMapping("/upload")
    public String fileupload(MultipartFile file, HttpServletRequest req) throws FileNotFoundException, UnsupportedEncodingException {
        String originName = file.getOriginalFilename();
        if(!originName.endsWith(".pdf")){
            return "文件格式错误";
        }
        String realPath = ResourceUtils.getURL("classpath:").getPath()+"static/";
        realPath = URLDecoder.decode(realPath,"utf-8");
//        String realPath = req.getServletContext().getRealPath("/");
        File folder = new File(realPath);
        if(!folder.exists()){
            folder.mkdirs();
        }
        String newName = UUID.randomUUID().toString()+".pdf";
        try {
            file.transferTo(new File(folder,newName));
//            String url = req.getScheme() + "://" + req.getServerName() + ":" + req.getServerPort() + "-" + newName;
            String url = realPath + newName;
            String str1 = url.substring(0, url.indexOf("/target/classes/static/"));
            String splicedURL = url.substring(str1.length()+1, url.length());
            return splicedURL;
        } catch (IOException e) {
            System.out.println(e);
        }
        return "未知错误";
    }

    @GetMapping("/getCurrentPath")
    public String getCurrentPath(){
        return System.getProperty("user.dir");
    }

//    @GetMapping("/getFile")
//    public void getFile(String filePath, HttpServletResponse response) throws IOException {
//        System.out.println(filePath);
//        String path = filePath;
//        response.setHeader("content-disposition","attachment;fileName="+"download.pdf");
//        InputStream in = new FileInputStream(path); //获取下载文件的输入流
//        int count =0;
//        byte[] by = new byte[1024];
//        //通过response对象获取OutputStream流
//        OutputStream out = response.getOutputStream();
//        while((count=in.read(by))!=-1){
//            out.write(by, 0, count);//将缓冲区的数据输出到浏览器
//        }
//        in.close();
//        out.flush();
//        out.close();
//
//    }

    @GetMapping("/outPut")
    public void outPutLocal(String path, HttpServletRequest request, HttpServletResponse response) throws Exception {

        System.out.println(path);
//        File file = new File(path.replace("/", "\\"));
        File file = new File(path);
        FileInputStream is = new FileInputStream(file);

        response.setContentType("application/pdf;charset=UTF-8");
        OutputStream out = response.getOutputStream();

        byte[] b = new byte[1024];
        int read;
        while ((read = is.read(b)) != -1) {
            out.write(b, 0, read);
        }
        is.close();
        out.flush();
        out.close();
    }

}
